package com.cesc.demo.config;

import com.cesc.demo.mode.Product;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory(){
        return new LettuceConnectionFactory();
    }

    @Bean
    public ReactiveRedisTemplate<String,String> reactiveRedisTemplate(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory){
        return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, RedisSerializationContext.string());
    }
    @Bean
    public ReactiveRedisTemplate<String, Product> redisOperations(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory){
        Jackson2JsonRedisSerializer<Product> serializer=new Jackson2JsonRedisSerializer<>(Product.class);
        RedisSerializationContext.RedisSerializationContextBuilder<String,Product> builder=
                RedisSerializationContext.newSerializationContext(new StringRedisSerializer());
        RedisSerializationContext<String,Product> context=builder.value(serializer).build();
        return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory,context);
    }
}
